package com.visualexpress.data;

public class Time implements Details, Cloneable {

	int year;
	int month;
	int day;
	int hour;

	public static final int JANUARY = 1;
	public static final int FENRUARY = 2;
	public static final int MARCH = 3;
	public static final int APRIL = 4;
	public static final int MAY = 5;
	public static final int JUNE = 6;
	public static final int JULY = 7;
	public static final int AUGUST = 8;
	public static final int SEPTEMBER = 9;
	public static final int OCTOBER = 10;
	public static final int NOVEMBER = 11;
	public static final int DECEMBER = 12;

	public int getYear() {
		
		return year;
	}

	public int getMonth() {
		
		return month;
	}

	public int getDay() {
		
		return day;
	}

	public int getHour() {
		
		return hour;
	}

	private void setYear(int year) {
		
		this.year = year;
	}

	private void setMonth(int month) {
		
		this.month = month;
	}

	private void setDay(int day) {
		
		this.day = day;
	}

	private void setHour(int hour) {
		
		this.hour = hour;
	}

	public static void main(String[] args) {

		System.out.println(" max : " + Integer.MAX_VALUE);
		System.out.println(" min : " + Integer.MIN_VALUE);

	}//end of main()

	public Time(int year, int month, int day, int hour) {

		this.setYear(0);
		this.setMonth(0);
		this.setDay(0);
		this.setHour(0);

		this.addHour(hour);
		this.addDay(day);
		this.addMonth(month);
		this.addYear(year);
	}//end of Date()

	private void addHour(int hour) {

		if (this.hour + hour < 24) {
			this.setHour(this.hour + hour);
		} else {
			int extra = (this.hour + hour) / 24;
			this.setHour((this.hour + hour) % 24);
			this.addDay(extra);
		}
	}//end of addHour()

	private int maxDaysInMonth(int month) {

		if (month == 0) {
			month = getMonth();
		}

		switch (month) {
		case JANUARY:
		case MARCH:
		case MAY:
		case JULY:
		case AUGUST:
		case OCTOBER:
		case DECEMBER:
			return 31;
		case FENRUARY:
			return 28;
		default:
			return 30;
		}
	}//end of maxDaysInMonth()

/*	private void addDay(int day) {

		if (this.day + day <= maxDaysInMonth(0)) {
			this.setDay(this.day + day);
		} else {
			this.addMonth(1);
			this.addDay(this.day + day - maxDaysInMonth(0));
		}
	}//end of addDay()
*/
	
	private void addDay(int day){
		
		int year = day / 365;
		if(year > 0){
			this.setYear(year);
			day -= year * 365;
			this.addDay(day);
		} else if (this.day + day <= maxDaysInMonth(0)){
			this.setDay(this.day + day);
		} else {
			this.addMonth(1);
			this.addDay(day - maxDaysInMonth(0));
		}
	}//end of addDay()
	
	private void addMonth(int month) {

		if (this.month + month <= 12) {
			this.setMonth(this.month + month);
		} else {
			this.setMonth(this.month + month - 12);
			this.addYear(1);
		}
	}//end of addMonth()

	private void addYear(int year) {

		if (this.year + year <= 2008) {
			this.setYear(this.year + year);
		} else {
			this.setYear(2007);
		}
	}//end of addYear()

	public void addTime(int hour) {

		this.addHour(hour);

	}//end of addTime;

	public int numOfDays() {

		int days = getYear() * 365;
		for (int j = 1; j < getMonth(); j++) {
			days += maxDaysInMonth(j);
		}
		days += getDay() - 1;

		return days;
	}//end of numOfDays()
	
	public int numOfHours(){
		
		int hours = numOfDays() * 24 + getHour();
		
		return hours;
	}

	public Time clone() {

		Time temp = new Time(this.year, this.month, this.day, this.hour);

		return temp;

	}//end of clone

	public String toString() {
		return this.getYear() + " - " + this.getMonth() + " - " + this.getDay()
				+ " : " + this.getHour();
	}

	public void showDetail() {

		System.out.println(this.getYear() + " - " + this.getMonth() + " - "
				+ this.getDay() + " : " + this.getHour());

	}//end of showDetail

}
